// Project18.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include"pch.h"
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<Windows.h>
int position_x, position_y;//飞机位置
int bu_x, bu_y;//子弹位置
int en_x, en_y;//敌机位置
int high, width;//全局变量
int score;
void start()
{
	high = 18;
	width = 30;
	position_x = high / 2;
	position_y = width / 2;
	bu_x = -1;
	bu_y= position_y;
	en_x = 0;
	en_y = width/2;
	score = 0;
}
void show()
{
	int i = 0, j = 0;
	system("cls");//清屏
	for (i = 0; i <= high; i++)
	{
		for (j = 0; j <= width; j++)
		{
			if ((i == position_x) && (j == position_y))
				printf("*");//飞机
			else if ((i == bu_x) && (j == bu_y))
				printf("|");//子弹
			else if ((i == en_x) && (j == en_y))
				printf("@");//敌机
			else
				printf(" ");
		}
		printf("\n");
	}
	printf("score:%d\n", score);
}
void updateWithoutInput()
{
	if ((bu_x == en_x) && (bu_y == en_y))
	{
		score++;
		en_y = rand() % width;
		en_x = 0;
	}
	static int speed = 0;//控制飞机速度
	if (speed < 10)
		speed++;
	if (bu_x > -1)
		bu_x--;
	if (en_x > high)
	{
		en_x = 0;
		en_y = rand() % width;
	}
	else
	{
		if (speed == 10)
		{
			en_x++;
			speed = 0;
		}
	}

}
void updateWithInput()
{
	char Input;
	if (kbhit())//输入
	{
		Input = getch();
		if (Input == 'a')
			position_y--;
		if (Input == 'd')
			position_y++;
		if (Input == 'w')
			position_x--;
		if (Input == 's')
			position_x++;
		if (Input == ' ')
		{
			bu_x = position_x - 1;
			bu_y = position_y;
		}
	}
}
int main()
{
	start();//定义变量

	while (1)//循环
	{
		show();//展示画面
		updateWithoutInput();//与输入无关的更新
		updateWithInput();//与输入有关的更新
	}
	return 0;
}
